<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\mcn;

use think\Model;

/**
 * MCN 艺人 模型
 * @package app\user_center\model\mcn
 */
class UcAttestationArtist extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_attestation_artist';

    public static function init()
    {
        self::event('after_insert', function ($user) {
            //机构艺人数增加
            model('UcAttestation', 'model\mcn') -> incArtistTimes($user->attestation_id);
        });
        self::event('after_delete', function ($user) {
            //机构艺人数减少
            model('UcAttestation', 'model\mcn') -> decArtistTimes($user->attestation_id);
        });
    }

    /**
     * 获取 艺人列表数据
     * @param 
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getList($map){
        $list = self::withJoin('getUcAttestation,getVideoPlayClass', 'LEFT') -> where($map) -> order('create_time desc') -> paginate();
        return array_out(200, 'success', $list);
    }

    /**
     * 添加艺人数据
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function addData($uid, $attestation_id, $artist_name, $artist_status, $classid){
        $this -> uid                         = $uid;
        $this -> attestation_id              = $attestation_id;
        $this -> artist_name                 = $artist_name;
        $this -> artist_status               = $artist_status;
        $this -> classid                     = $classid;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
    
    /**
     * 获取 艺人绑定信息
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getDataByUid($uid){
        $map = [];
        $map['uid'] = $uid;
        $info = self::where($map) -> find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 机构 关联载入（一对一）
     */
    public function getUcAttestation(){
        return $this -> hasOne('app\user_center\model\mcn\UcAttestation','id', 'attestation_id');
    }
	
	/**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMemberCount(){
        return $this -> hasOne('app\user_center\model\user\UcMemberCount','uid', 'uid');
    }

    /**
     * 分类 关联载入（一对一）
     */
    public function getVideoPlayClass(){
        return $this -> hasOne('app\video\model\VideoPlayClass','id', 'classid');
    }

    /**
     * 手机号 关联载入（一对一）
     */
    public function getUcMemberMobile(){
        return $this -> hasOne('app\user_center\model\user\UcMemberMobile','uid', 'uid');
    }
}
